﻿using System;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace Gui.Functies.Login
{
    public partial class Login : Form
    {
        private static string conn = ConfigurationManager.ConnectionStrings["con1"].ConnectionString;
        MySqlConnection grabConnection = new MySqlConnection(conn);
        public static int userID;
        public static int projectAmount;

        //hier komen title en description voor de projecten in
        public static string[] title = new string[100];
        public static string[] description = new string[100];
        public static int[] ids = new int[100];

        //hier komen de project ID's in
        int[] projects = new int[10];
        MySqlDataReader reader = null;

        //Constructor
        public Login()
        {
            InitializeComponent();
            label1.Hide();
            progressBar1.Hide();
            label2.Hide();     
        }
        //Methode om de username/password te checken.
        public Boolean LoginCheck(string email, string wachtwoord)
        {
            //methode die checkt of de gebruiker wel / niet in mag loggen
            string username = email;
            string password = wachtwoord;

            try
            {
                //connectie naar server + query
                MySqlConnection grabConnection = new MySqlConnection("Server=qq-design.com;Port=3306;Database=cookicom0;Uid=cookicom0;pwd=pizzahut91");
                MySqlCommand login = new MySqlCommand("SELECT Email, Wachtwoord FROM confirmed_users WHERE Email='" + username + "' AND Wachtwoord='" + password + "'", grabConnection);
                grabConnection.Open();

                //data lezen
                MySqlDataReader reader = login.ExecuteReader();

                if (reader.Read() != false)
                {
                    if (reader.IsDBNull(0) == true)
                    {
                        //alles sluiten, login is gefaald
                        grabConnection.Close();
                        reader.Dispose();
                        login.Dispose();
                        grabConnection.Close();
                        return false;
                    }
                    else
                    {
                        //login geslaagd!   
                        grabConnection.Close();
                        reader.Dispose();
                        login.Dispose();
                        grabConnection.Close();
                        return true;
                    }
                }
                else
                {
                    grabConnection.Close();
                    return false;
                }
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.ToString());
                return false;
            }
        }
        //Start het login process.
        private void btnLogin_Click(object sender, EventArgs e)
        {
            //doorgegeven parameters
            String name = txtUsername.Text;
            String pass = txtPassword.Text;
            progressBar1.Minimum = 0;
            progressBar1.Maximum = 100;
            progressBar1.Step = 18;
            //login = waar -> doorsturen naar loadbar
            if (LoginCheck(name, pass) == true)
            {

                txtUsername.Hide();
                txtPassword.Hide();
                label1.Show();
                progressBar1.Show();
                btnLogin.Hide();

                try
                {
                    label2.Show();
                    label2.Text = "Getting Users and projects";
                    grabConnection.Open();
                    //userID ophalen
                    MySqlCommand getID = new MySqlCommand("SELECT UserID FROM confirmed_users WHERE Email='" + name + "'", grabConnection);
                    MySqlDataReader reader = getID.ExecuteReader();

                    //userID ophalen
                    while (reader.Read())
                    {
                        userID = Convert.ToInt32(reader["userID"].ToString());
                    }

                    grabConnection.Close();

                    grabConnection.Open();

                    MySqlCommand getProjectAmount = new MySqlCommand("SELECT COUNT(UserID) AS AMOUNT FROM project_members WHERE UserID='" + userID + "'", grabConnection);
                    reader = getProjectAmount.ExecuteReader();

                    //aantal projecten ophalen
                    while (reader.Read())
                    {
                        projectAmount = Convert.ToInt32(reader["AMOUNT"].ToString());
                    }

                    grabConnection.Close();

                    grabConnection.Open();

                    MySqlCommand getProjectID = new MySqlCommand("SELECT ProjectID FROM project_members WHERE UserID='" + userID + "' ORDER BY ProjectID", grabConnection);
                    reader = getProjectID.ExecuteReader();

                    //projectIDID ophalen
                    int counter = 0;
                    while (reader.Read())
                    {
                        projects[counter] = Convert.ToInt32(reader["ProjectID"].ToString());
                        counter++;
                    }

                    grabConnection.Close();
                }
                catch (MySqlException u)
                {
                    MessageBox.Show(u.ToString());
                    return;
                }
                ///////////////////////////////////////////////////////////////////////////////////////////
                progressBar1.PerformStep();
                label2.Text = "Getting project information";
                ///////////////////////////////////////////////////////////////////////////////////////////
                if (projectAmount >= 1)
                {
                    try
                    {
                        //projecttitel en beschrijving ophalen
                        grabConnection.Open();
                        MySqlCommand getProjectInfo = new MySqlCommand("SELECT ProjectID, Projectnaam, Projectbeschrijving FROM projects WHERE ProjectID ='" + projects[0] + "'", grabConnection);
                        reader = getProjectInfo.ExecuteReader();

                        while (reader.Read())
                        {
                            string id = reader["ProjectID"].ToString();
                            ids[0] = Int32.Parse(id);
                            title[0] = reader["Projectnaam"].ToString();
                            description[0] = reader["Projectbeschrijving"].ToString();
                        }
                        reader.Dispose();
                    }
                    catch (MySqlException u)
                    {
                        MessageBox.Show("Sql error detected! @ project 1" + u.ToString());

                    }
                }
                //////////////////////////////////////////////////////////////////////////////////////////
                progressBar1.PerformStep();
                ///////////////////////////////////////////////////////////////////////////////////////////
                if (projectAmount >= 2)
                {
                    try
                    {
                        //projecttitel en beschrijving ophalen
                        MySqlCommand getProjectInfo = new MySqlCommand("SELECT ProjectID, Projectnaam, Projectbeschrijving FROM projects WHERE ProjectID ='" + projects[1] + "'", grabConnection);
                        MySqlDataReader reader2 = getProjectInfo.ExecuteReader();

                        while (reader2.Read())
                        {
                            string id = reader2["ProjectID"].ToString();
                            ids[1] = Int32.Parse(id);
                            title[1] = reader2["Projectnaam"].ToString();
                            description[1] = reader2["Projectbeschrijving"].ToString();
                        }
                        reader2.Dispose();
                    }
                    catch (MySqlException u)
                    {
                        MessageBox.Show("Sql error detected! @ project 2" + u.ToString());
                    }
                }
                ///////////////////////////////////////////////////////////////////////////////////////////
                progressBar1.PerformStep();
                ///////////////////////////////////////////////////////////////////////////////////////////
                if (projectAmount >= 3)
                {
                    try
                    {
                        //projecttitel en beschrijving ophalen
                        MySqlCommand getProjectInfo = new MySqlCommand("SELECT ProjectID, Projectnaam, Projectbeschrijving FROM projects WHERE ProjectID ='" + projects[2] + "'", grabConnection);
                        MySqlDataReader reader3 = getProjectInfo.ExecuteReader();

                        while (reader3.Read())
                        {
                            string id = reader3["ProjectID"].ToString();
                            ids[2] = Int32.Parse(id);
                            title[2] = reader3["Projectnaam"].ToString();
                            description[2] = reader3["Projectbeschrijving"].ToString();
                        }
                        reader3.Dispose();
                    }
                    catch (MySqlException u)
                    {
                        MessageBox.Show("Sql error detected! @ project 3" + u.ToString());
                    }
                }
                ///////////////////////////////////////////////////////////////////////////////////////////
                progressBar1.PerformStep();
                ///////////////////////////////////////////////////////////////////////////////////////////
                if (projectAmount >= 4)
                {
                    try
                    {
                        //projecttitel en beschrijving ophalen
                        MySqlCommand getProjectInfo = new MySqlCommand("SELECT ProjectID, Projectnaam, Projectbeschrijving FROM projects WHERE ProjectID ='" + projects[3] + "'", grabConnection);
                        MySqlDataReader reader4 = getProjectInfo.ExecuteReader();

                        while (reader4.Read())
                        {
                            string id = reader4["ProjectID"].ToString();
                            ids[3] = Int32.Parse(id);
                            title[3] = reader4["Projectnaam"].ToString();
                            description[3] = reader4["Projectbeschrijving"].ToString();
                        }
                        reader4.Dispose();
                    }
                    catch (MySqlException u)
                    {
                        MessageBox.Show("Sql error detected! @ project 4" + u.ToString());
                    }
                }
                ///////////////////////////////////////////////////////////////////////////////////////////
                progressBar1.PerformStep();
                ///////////////////////////////////////////////////////////////////////////////////////////
                if (projectAmount == 5)
                {
                    try
                    {
                        //projecttitel en beschrijving ophalen
                        MySqlCommand getProjectInfo = new MySqlCommand("SELECT ProjectID, Projectnaam, Projectbeschrijving FROM projects WHERE ProjectID ='" + projects[4] + "'", grabConnection);
                        MySqlDataReader reader5 = getProjectInfo.ExecuteReader();

                        while (reader5.Read())
                        {
                            string id = reader5["ProjectID"].ToString();
                            ids[4] = Int32.Parse(id);
                            title[4] = reader5["Projectnaam"].ToString();
                            description[4] = reader5["Projectbeschrijving"].ToString();
                        }
                        reader5.Dispose();
                        grabConnection.Close();
                    }
                    catch (MySqlException u)
                    {
                        MessageBox.Show("Sql error detected! @ project 5" + u.ToString());
                    }
                }

                progressBar1.PerformStep();
                label2.Text = "Done!";
                if (progressBar1.Value == 100)
                {
                    this.DialogResult = DialogResult.OK;
                }
            }
            //login = false -> doorsturen naar error screen
            else
            {
                MessageBox.Show("Failed to log in");
            }
        }
    }
}
